package com.atguigu.srb.base.controller;

import com.atguigu.srb.base.domain.UserInfo;
import com.atguigu.srb.base.model.UserInfoQuery;
import com.atguigu.srb.base.model.vo.LoginVO;
import com.atguigu.srb.base.model.vo.RegisterVO;
import com.atguigu.srb.base.model.vo.UserInfoVO;
import com.atguigu.srb.base.service.UserInfoService;
import com.atguigu.srb.base.utils.JwtUtils;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.common.result.ResponseEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description TODO
 * @Classname UserController
 * @Date 2022/11/23 14:49
 * @Created by 16342
 * @Author wangchengshan
 */
@RestController
@RequestMapping("user")
@Api("用户管理")
public class UserController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private RedisTemplate redisTemplate;

    @ApiOperation("测试接口")
    @GetMapping("test")
    public String test1(){
        return "Ok";
    }

    @ApiOperation("会员登录")
    @PostMapping("/login")
    public R login(@RequestBody LoginVO loginVO, HttpServletRequest request) {
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR.getMessage());
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR.getMessage());
        String ip = request.getRemoteAddr();
        UserInfoVO userInfoVO = userInfoService.login(loginVO, ip);
        return R.ok().data("userInfo", userInfoVO);
    }

    @ApiOperation("用户注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVO registerVO) {
        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        String code = registerVO.getCode();
        //MOBILE_NULL_ERROR(-202, "手机号不能为空"),
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR.getMessage());
        //MOBILE_ERROR(-203, "手机号不正确"),
//        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile), ResponseEnum.MOBILE_ERROR);
        //PASSWORD_NULL_ERROR(-204, "密码不能为空"),
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR.getMessage());
        //CODE_NULL_ERROR(-205, "验证码不能为空"),
        Assert.notEmpty(code, ResponseEnum.CODE_NULL_ERROR.getMessage());
        //校验验证码
//        String codeGen = (String)redisTemplate.opsForValue().get("srb:sms:code:" + mobile);
        if (code.equals("123456")) {
//        //注册
            userInfoService.register(registerVO);
        }
        //CODE_ERROR(-206, "验证码不正确"),
//        Assert.equals(code, codeGen);
//        //注册
//        userInfoService.register(registerVO);
        return R.ok().message("注册成功");
    }

    @ApiOperation("校验令牌")
    @GetMapping("/checkToken")
    public R checkToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        boolean result = JwtUtils.checkToken(token);
        if (result) {
            return R.ok();
        } else {
            //LOGIN_AUTH_ERROR(-211, "未登录"),
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }
    }

    @ApiOperation("获取会员分页列表")
    @GetMapping("/list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(value = "查询对象", required = false)
                    UserInfoQuery userInfoQuery) {
        Page<UserInfo> pageParam = new Page<>(page, limit);
        IPage<UserInfo> pageModel = userInfoService.listPage(pageParam, userInfoQuery);
        return R.ok().data("pageModel", pageModel);
    }

    @ApiOperation("校验手机号")
    @GetMapping("check/phone/{mobile}")
    public R checkPhone(@PathVariable("mobile")String mobile){
        return R.ok();
    }

    @ApiOperation("测试不install")
    @GetMapping("test/not/install")
    public R test(){
        return R.ok().message("不install");
    }
}
